package com.lianboke.link.ad.dbmanager;

import android.text.TextUtils;

import com.lianboke.link.ad.beans.OrdetTypeDetailsBean;
import com.lianboke.link.ad.entry.OrdetTypeTable;
import com.lianboke.link.ad.utils.DateUtil;
import com.lianboke.link.ad.utils.TimestampTool;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 订单详情工具类
 * Created by admin on 2018/1/7.
 */

public class OrderTypeUtils {

    /**
     * 订单详情转换订单数据库
     * @param originalBean
     * @return
     */
    public static OrdetTypeTable OrderTypeDetailSwitchOrderTypeTable(OrdetTypeDetailsBean originalBean){
        OrdetTypeTable targetBean =new OrdetTypeTable();
        if(originalBean==null){
            return null;
        }else{
            targetBean.setId(originalBean.getId());
            targetBean.setName(originalBean.getName());
            targetBean.setImg(originalBean.getImg());
            targetBean.setDesc(originalBean.getDesc());
            targetBean.setType(originalBean.getType());
            targetBean.setMemo(originalBean.getMemo());
            targetBean.setPrice(originalBean.getPrice());
            targetBean.setInterval(originalBean.getInterval());
            targetBean.setIntervaltype(originalBean.getIntervaltype());
            targetBean.setDuration(originalBean.getDuration());
            targetBean.setAppdownload(originalBean.getAppdownload());
            targetBean.setAppname(originalBean.getAppname());
            targetBean.setStudyurl(originalBean.getStudyurl());
            targetBean.setEntryurl(originalBean.getEntryurl());
            //时间转换
            String strCurrentTime = DateUtil.currentDatetime();
            String strCurrentDate = DateUtil.getCurDateStr("yyyy-MM-dd");
            targetBean.setCreateTime(strCurrentTime);
            targetBean.setFirstLaunchTime(strCurrentDate);//首次启动时间
            targetBean.setLastLaunchTime(strCurrentDate);//最后的启动时间
//            targetBean.setFirstLaunchTime(DateUtil.getCurDateStr("yyyy-MM-dd"));//首次启动时间
//            targetBean.setLastaunchTime(DateUtil.getCurDateStr("yyyy-MM-dd"));//最后的启动时间

            //设置需要使用的时间
            if(originalBean.getIntervaltype()==1){
                //1:连续使用
                int durationDate = originalBean.getDuration();
                if(durationDate<=0){
                    //仅需要首次使用
                    targetBean.setDurationLaunchTimes("");
                }else{
                    //需要连续多天使用
                    Set<String> dateLists = new HashSet<String>();
                    for(int j=0;j<=durationDate;j++){
                        String strDate = TimestampTool.getCurrentDateNextValues(j);
                        if(!TextUtils.isEmpty(strDate)){
                            dateLists.add(strDate);
                        }
                    }
                    //将使用天数格式化
                    StringBuffer strDurationDate = new StringBuffer();
                    if(dateLists!=null && !dateLists.isEmpty()){
                        for (String strcontent:dateLists) {
                            strDurationDate.append(strcontent);
                            strDurationDate.append(",");
                        }
                       int lastIndex =  strDurationDate.lastIndexOf(",");
                        strDurationDate.deleteCharAt(lastIndex);
                    }
                    //需要启动时间
                    targetBean.setDurationLaunchTimes(strDurationDate.toString());
                }
            }else if(originalBean.getIntervaltype()==2){
                //间隔使用
                int intervalDate= originalBean.getInterval();//间隔时间
                int duration_interval_date = originalBean.getDuration();//使用时间
                if(intervalDate==0 && duration_interval_date==0){
                    //仅需要首次使用
                    targetBean.setDurationLaunchTimes("");
                }else if(duration_interval_date>0){
                    //连续使用
                    Set<String> dateLists_1 = new HashSet<String>();
                    dateLists_1.add(DateUtil.getCurDateStr("yyyy-MM-dd"));//加入今天
                    for(int j=0;j<=duration_interval_date;j++){
                        String strDate = TimestampTool.getCurrentDateNextValues(intervalDate+j);
                        if(!TextUtils.isEmpty(strDate)){
                            dateLists_1.add(strDate);
                        }
                    }
                    //将使用天数格式化
                    StringBuffer strDurationDate_1 = new StringBuffer();
                    if(dateLists_1!=null && !dateLists_1.isEmpty()){
                        for (String strcontent:dateLists_1) {
                            strDurationDate_1.append(strcontent);
                            strDurationDate_1.append(",");
                        }
                        int lastIndex =  strDurationDate_1.lastIndexOf(",");
                        strDurationDate_1.deleteCharAt(lastIndex);
                    }
                    //需要启动时间
                    targetBean.setDurationLaunchTimes(strDurationDate_1.toString());
                }
            }
        }
        return targetBean;
    }

    /**
     *  数据库转化为订单详情
     * @param originalBean
     * @return
     */
    public static OrdetTypeDetailsBean OrderTypeDetailSwitchOrderTypeTable(OrdetTypeTable originalBean){
        OrdetTypeDetailsBean targetBean =new OrdetTypeDetailsBean();
        if(originalBean==null){
            return null;
        }else{
            targetBean.setId(originalBean.getId());
            targetBean.setName(originalBean.getName());
            targetBean.setImg(originalBean.getImg());
            targetBean.setDesc(originalBean.getDesc());
            targetBean.setType(originalBean.getType());
            targetBean.setMemo(originalBean.getMemo());
            targetBean.setPrice(originalBean.getPrice());
            targetBean.setInterval(originalBean.getInterval());
            targetBean.setIntervaltype(originalBean.getIntervaltype());
            targetBean.setDuration(originalBean.getDuration());
            targetBean.setAppdownload(originalBean.getAppdownload());
            targetBean.setAppname(originalBean.getAppname());
            targetBean.setStudyurl(originalBean.getStudyurl());
            targetBean.setEntryurl(originalBean.getEntryurl());
        }
        return targetBean;
    }
}
